﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Web.UI.WebControls.WebParts;
using Telerik.Web.UI;
using System.Configuration;

public partial class SiteWorx_WebParts_Basic_ContactUs : BaseWebPart
{
    private string _to = "";
    private string _cc = "";
    private string _bcc = "";

    [Personalizable(PersonalizationScope.Shared)]
    public string To
    {
        get
        {
            return _to;
        }
        set
        {
            _to = value;
        }
    }

    [Personalizable(PersonalizationScope.Shared)]
    public string CC
    {
        get
        {
            return _cc;
        }
        set
        {
            _cc = value;
        }
    }

    [Personalizable(PersonalizationScope.Shared)]
    public string Bcc
    {
        get
        {
            return _bcc;
        }
        set
        {
            _bcc = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            pnlEditContentButton.Visible = Page.User.IsInRole("Administrator") && !Utility.PageInPreviewMode();
    }

    protected void btnSend_Click(object sender, EventArgs e)
    {
        SendEmail();
    }

    private void SendEmail()
    {
        string message = "";

        if (To.Length <= 0 && CC.Length <= 0 && Bcc.Length <= 0)
            message = "Cannot send message because there is no email recipient specified.";
        else
        {
            if (txtName.Text == "")
                message = "Name required.";
            else if (txtEmail.Text == "")
                message = "Email required.";
            else if (txtSubject.Text == "")
                message = "Subject required.";
            else if (txtMessage.Text == "")
                message = "Message required.";
            else
            {
                SmtpClient client = new SmtpClient();
                MailMessage email = new MailMessage();

                char[] splitter = {';'};
                string[] to = To.Split(splitter);
                string[] cc = CC.Split(splitter);
                string[] bcc = Bcc.Split(splitter);

                if (To.Length > 0)
                    foreach (string address in to)
                        email.To.Add(address);
                
                if (CC.Length > 0)
                    foreach (string address in cc)
                        email.CC.Add(address);

                if (Bcc.Length > 0)
                    foreach (string address in bcc)
                        email.Bcc.Add(address);

                email.From = new MailAddress(txtEmail.Text);
                email.Subject = txtSubject.Text;
                email.Body = txtMessage.Text;

                try
                {
                    client.Send(email);
                    txtName.Text = "";
                    txtEmail.Text = "";
                    txtSubject.Text = "";
                    txtMessage.Text = "";
                    message = "Your email was successfully submitted!";
                }
                catch (Exception ex)
                {
                    message = ex.Message;
                }
            }
        }
        lblMessage.Text = message;
        lblMessage.Visible = true;
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        RemoveWebPart();
    }

    protected override void RemoveWebPart()
    {
        base.RemoveWebPart();
    }
    protected void lnkbtnUpdateContactUsWPData_Click(object sender, EventArgs e)
    {
        string contactUsWPDataId = hidContactUsWPDataId.Value;
        ContactUsWebPartData data = (ContactUsWebPartData)Session[contactUsWPDataId];
        this.To = data.To;
        this.CC = data.CC;
        this.Bcc = data.BCC;
       
        //clear the content id and session variable
        hidContactUsWPDataId.Value = "";
        Session[contactUsWPDataId] = null;
    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        string contactUsWPDataId = Guid.NewGuid().ToString() + "_ContactUs";
        Session[contactUsWPDataId] = new ContactUsWebPartData(this.To, this.CC, this.Bcc);
        hidContactUsWPDataId.Value = contactUsWPDataId;

        Telerik.Web.UI.RadWindow contactUsEditor = new Telerik.Web.UI.RadWindow();
        contactUsEditor.NavigateUrl = "~/SiteWorx/WebParts/Basic/ContactUs/ContactUsEditor.aspx?contactUsWPDataId=" + contactUsWPDataId;
        contactUsEditor.VisibleOnPageLoad = true;
        contactUsEditor.Modal = true;
        contactUsEditor.Behaviors = WindowBehaviors.Move | WindowBehaviors.Resize;
        contactUsEditor.ShowContentDuringLoad = false;
        contactUsEditor.VisibleStatusbar = false;
        contactUsEditor.Width = Unit.Pixel(595);
        contactUsEditor.Height = Unit.Pixel(400);

        pnlEditContentButton.Controls.Add(contactUsEditor);

        BuildUpdateScript();
    }

    private void BuildUpdateScript()
    {
        string contactUsWPDataId = hidContactUsWPDataId.Value;
        if (contactUsWPDataId.Length > 0)
        {
            string functionName = "Update_" + contactUsWPDataId.Replace("-", "_");
            string script = "function " + functionName + "(sender){__doPostBack('" + lnkbtnUpdateContactUsWPData.UniqueID + "','')}";
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), functionName, script, true);
        }
    }
}
